class ClickTimerApp:
    """美化版点击倒计时应用"""
    def __init__(self, parent, delay_seconds, interval_seconds, is_limited, total_clicks=None):
        self.parent = parent
        self.parent.title("鼠标点击模拟器")
        self.parent.attributes('-topmost', True)  # 窗口置顶
        self.parent.resizable(False, False)
        self.parent.configure(bg="#1E1E1E")
        
        # 设置中文字体支持
        self.font_family = ("SimHei", "Microsoft YaHei", "Arial")
        
        # 数据
        self.delay_seconds = delay_seconds
        self.interval_seconds = interval_seconds
        self.is_limited = is_limited
        self.total_clicks = total_clicks
        self.current_click = 0
        self.time_remaining = delay_seconds
        self.state = "DELAY"  # DELAY, CLICKING, PAUSED
        
        # 创建UI
        self.create_widgets()
        
        # 启动倒计时
        self.update_countdown()
    
    def create_widgets(self):
        """创建UI组件"""
        # 主框架
        main_frame = tk.Frame(self.parent, bg="#1E1E1E")
        main_frame.pack(padx=20, pady=20, fill=tk.BOTH, expand=True)
        
        # 标题
        title_label = tk.Label(
            main_frame, 
            text="鼠标点击模拟器", 
            font=(self.font_family[0], 16, "bold"),
            bg="#1E1E1E",
            fg="#FFFFFF"
        )
        title_label.pack(pady=(0, 15))
        
        # 倒计时显示
        self.countdown_var = tk.StringVar(value=f"准备开始: {self.time_remaining:.1f}秒")
        self.countdown_label = tk.Label(
            main_frame, 
            textvariable=self.countdown_var, 
            font=(self.font_family[0], 36, "bold"),
            bg="#1E1E1E",
            fg="#4CAF50"
        )
        self.countdown_label.pack(pady=10)
        
        # 点击进度
        self.progress_var = tk.StringVar(value="")
        self.progress_label = tk.Label(
            main_frame, 
            textvariable=self.progress_var, 
            font=(self.font_family[0], 12),
            bg="#1E1E1E",
            fg="#FFFFFF"
        )
        self.progress_label.pack(pady=5)
        
        # 进度条
        self.progress_bar = ttk.Progressbar(
            main_frame, 
            orient="horizontal",
            length=300,
            mode="determinate"
        )
        self.progress_bar.pack(pady=10)
        
        # 状态指示
        self.status_var = tk.StringVar(value="等待开始")
        self.status_label = tk.Label(
            main_frame, 
            textvariable=self.status_var, 
            font=(self.font_family[0], 12),
            bg="#1E1E1E",
            fg="#FFC107"
        )
        self.status_label.pack(pady=5)
        
        # 底部信息
        info_label = tk.Label(
            main_frame, 
            text="按 Ctrl+Alt+C 停止 | ESC 退出程序", 
            font=(self.font_family[0], 10),
            bg="#1E1E1E",
            fg="#888888"
        )
        info_label.pack(side=tk.BOTTOM, pady=10)
        
        # 设置进度条样式
        self.style = ttk.Style()
        self.style.theme_use('clam')
        self.style.configure("green.Horizontal.TProgressbar", 
                            background="#4CAF50", 
                            troughcolor="#303030",
                            borderwidth=0)
        self.progress_bar.configure(style="green.Horizontal.TProgressbar")
        
        # 初始化进度条
        if self.is_limited:
            self.progress_bar["maximum"] = self.total_clicks
            self.progress_bar["value"] = 0
            self.update_progress()
    
    def update_progress(self):
        """更新进度显示"""
        if self.is_limited:
            self.progress_var.set(f"点击进度: {self.current_click}/{self.total_clicks}")
            self.progress_bar["value"] = self.current_click
        else:
            self.progress_var.set(f"点击次数: {self.current_click}")
    
    def update_countdown(self):
        """更新倒计时显示"""
        if self.state == "DELAY":
            # 首次点击前的延迟
            self.countdown_var.set(f"准备开始: {self.time_remaining:.1f}秒")
            self.time_remaining -= 0.1
            
            if self.time_remaining <= 0:
                self.state = "CLICKING"
                self.time_remaining = self.interval_seconds
                self.countdown_var.set(f"下一次点击: {self.time_remaining:.1f}秒")
                self.status_var.set("正在点击")
                self.current_click += 1
                self.update_progress()
            else:
                self.parent.after(100, self.update_countdown)
                
        elif self.state == "CLICKING":
            # 点击间隔倒计时
            self.countdown_var.set(f"下一次点击: {self.time_remaining:.1f}秒")
            self.time_remaining -= 0.1
            
            if self.time_remaining <= 0:
                self.time_remaining = self.interval_seconds
                self.current_click += 1
                self.update_progress()
                
                # 检查是否达到限制
                if self.is_limited and self.current_click > self.total_clicks:
                    self.state = "PAUSED"
                    self.countdown_var.set("点击完成!")
                    self.status_var.set("已完成所有点击")
                    self.progress_bar.configure(style="green.Horizontal.TProgressbar")
                else:
                    self.parent.after(100, self.update_countdown)
            else:
                self.parent.after(100, self.update_countdown)
                
        elif self.state == "PAUSED":
            # 暂停状态，不更新
            pass
    
    def set_state(self, state):
        """设置应用状态"""
        self.state = state
        
        if state == "PAUSED":
            self.status_var.set("已暂停")
            self.countdown_var.set("已暂停")
            self.progress_bar.configure(style="Horizontal.TProgressbar")
        elif state == "CLICKING":
            self.status_var.set("正在点击")
            self.parent.after(100, self.update_countdown)
    
    def reset(self):
        """重置应用"""
        self.state = "DELAY"
        self.time_remaining = self.delay_seconds
        self.current_click = 0
        self.countdown_var.set(f"准备开始: {self.time_remaining:.1f}秒")
        self.status_var.set("等待开始")
        self.update_progress()
        self.parent.after(100, self.update_countdown)